import pandas as pd
import unittest
import os, sys
path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, path)
from contract_market.utils.api import xt_api
from contract_market.strategy.image.type import Side, DepthLevel, Action

symbol = 'btc_usd'
quote = 'btc'
transcation_type = 'cm'


class TestAPI(unittest.TestCase):

    # @unittest.skip
    def test_position(self):
        xt_api.init_client(symbol, transcation_type)
        res = xt_api.position(symbol, 'api')
        print('position', res)
    
    @unittest.skip
    def test_merge_position(self):
        xt_api.init_client(symbol, transcation_type)
        res = xt_api.merge_position(symbol)
        print('merge position', res)

    @unittest.skip
    def test_close_position(self):
        xt_api.init_client(symbol, transcation_type)
        res = xt_api.xt.close_all_position()
        print('close position', res)

    @unittest.skip
    def test_open_orders(self):
        xt_api.init_client(symbol, transcation_type)
        oor = xt_api.open_orders_api(symbol)
        oor = xt_api.oorder_side(oor)
        print(oor)
        print(oor[oor.side==Action.short].price.min(), oor[oor.side==Action.long].price.max())

    @unittest.skip
    def test_cancel_all(self):
        xt_api.init_client(symbol, transcation_type)
        print(xt_api.xt.cancel_all_order())

    @unittest.skip
    def test_cancel(self):
        id = 135085418486957696
        xt_api.init_client(symbol, transcation_type)
        res = xt_api.xt.cancel_order(order_id=id)
        print('CANCEL', res)

    @unittest.skip
    def test_send(self):
        xt_api.init_client(symbol, transcation_type)
        res = xt_api.xt.send_order(symbol=symbol, 
            price=7.472, amount=1000,
            order_side='SELL',
            order_type="LIMIT",
            position_side='SHORT', 
            timeInForce='GTC')
        print('SEND', res)
    
    # @unittest.skip
    def test_balance(self):
        xt_api.init_client(symbol, transcation_type)
        # res = xt_api.balance(method='api')
        res = xt_api.xt.get_coin_balance(quote)
        print('balance', res)

    @unittest.skip
    def test_spread(self):
        import numpy as np
        xt_api.init_client(symbol, transcation_type)
        xt_api.init_config(symbol)

        long_stat, short_stat = [], []
        for i in range(1000):
            index_price = xt_api.index_price(symbol)
            #
            oor = xt_api.open_orders_api(symbol, page=2)
            oor = xt_api.oorder_side(oor)
            long_maxp = oor[oor.side==Action.long].price.max()
            short_minp = oor[oor.side==Action.short].price.min()
            long_spread = (long_maxp-index_price)/index_price
            short_spread = (short_minp-index_price)/index_price
            print('spread', np.abs(long_spread), np.abs(short_spread))
            long_stat.append(long_spread)
            short_stat.append(short_spread)
        print('long stat', np.mean(np.abs(long_stat)), np.max(np.abs(long_stat)))
        print('short stat', np.mean(np.abs(short_stat)), np.max(np.abs(short_stat)))


if __name__ == '__main__':
    unittest.main()
